﻿package mortal.game.resource
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.resource.*;
    import flash.utils.*;
    import mortal.game.view.common.*;

    public class CopyConfig extends Object
    {
        private var _dic:Dictionary;
        private var _groupCopy:Dictionary;
        private var _singleCopy:Dictionary;
        private var _copyFitstBloodDic:Dictionary;
        private var _groupTowerCopy:Dictionary;
        private var _defenseCopy:Dictionary;
        private var _defenseArr:Array;
        private static var _instance:CopyConfig;

        public function CopyConfig()
        {
            this._dic = new Dictionary();
            this._groupCopy = new Dictionary();
            this._singleCopy = new Dictionary();
            this._copyFitstBloodDic = new Dictionary();
            this._groupTowerCopy = new Dictionary();
            this._defenseCopy = new Dictionary();
            this._defenseArr = [112, 128, 138];
            this.init();
            return;
        }// end function

        private function init() : void
        {
            var _loc_2:Object = null;
            var _loc_3:TCopy = null;
            this._dic = new Dictionary();
            var _loc_1:* = ConfigManager.instance.getJSONByFileName("t_copy.json");
            for each (_loc_2 in _loc_1)
            {
                
                _loc_3 = new TCopy();
                ClassTypesUtil.copyValue(_loc_3, _loc_2);
                this._dic[_loc_3.code] = _loc_3;
                if (_loc_3.type == ECopyType._ECopyTypeTeam && _loc_3.mode == ECopyMode._ECopyModeGroup)
                {
                    this._groupCopy[_loc_3.code] = _loc_3;
                }
                if (_loc_3.type == ECopyType._ECopyTypeSingle && _loc_3.mode == ECopyMode._ECopyModeSingle)
                {
                    this._singleCopy[_loc_3.code] = _loc_3;
                }
                if (_loc_3.type == ECopyType._ECopyTypeTower && _loc_3.mode == ECopyMode._ECopyModeGroup && _loc_3.group == 146)
                {
                    this._groupTowerCopy[_loc_3.code] = _loc_3;
                }
                if (_loc_3.type == ECopyType._ECopyTypeTower && _loc_3.mode == ECopyMode._ECopyModeSingle && _loc_3.group == 146)
                {
                    this._groupTowerCopy[_loc_3.code] = _loc_3;
                }
                if (this._defenseArr.indexOf(_loc_3.group) != -1)
                {
                    this._defenseCopy[_loc_3.code] = _loc_3;
                }
            }
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_copy_first_blood.json");
            this.writeFirstBlood(_loc_1);
            return;
        }// end function

        private function writeFirstBlood(param1:Object) : void
        {
            var _loc_2:Object = null;
            var _loc_3:TCopyFirstBlood = null;
            for each (_loc_2 in param1)
            {
                
                _loc_3 = new TCopyFirstBlood();
                ClassTypesUtil.copyValue(_loc_3, _loc_2);
                this._copyFitstBloodDic[_loc_3.copyCode] = _loc_3;
            }
            return;
        }// end function

        public function getCopyFirstBloodInfoByCode(param1:int) : TCopyFirstBlood
        {
            return this._copyFitstBloodDic[param1] as TCopyFirstBlood;
        }// end function

        public function getCopyInfoByCode(param1:int) : TCopy
        {
            return this._dic[param1] as TCopy;
        }// end function

        public function getGroupCopyByLevel(param1:int) : Array
        {
            var _loc_4:TCopy = null;
            var _loc_2:* = new Array();
            var _loc_3:* = new Array();
            for each (_loc_4 in this._groupCopy)
            {
                
                if (_loc_4.enterMinLevel <= param1 && param1 <= _loc_4.enterMaxLevel)
                {
                    _loc_2.push(_loc_4);
                    continue;
                }
                _loc_3.push(_loc_4);
            }
            _loc_2.sort(this.sortCopy);
            _loc_3.sort(this.sortCopy2);
            return _loc_2.concat(_loc_3);
        }// end function

        public function getGroupTowerCopy(param1:int) : Array
        {
            var _loc_4:TCopy = null;
            var _loc_2:* = new Array();
            var _loc_3:* = new Array();
            for each (_loc_4 in this._groupTowerCopy)
            {
                
                if (_loc_4.enterMinLevel <= param1 && param1 <= _loc_4.enterMaxLevel)
                {
                    _loc_2.push(_loc_4);
                    continue;
                }
                _loc_3.push(_loc_4);
            }
            _loc_2.sort(this.sortCopy);
            _loc_3.sort(this.sortCopy2);
            return _loc_2.concat(_loc_3);
        }// end function

        public function getGroupTowerCopyByLevel(param1:int) : Array
        {
            var _loc_3:TCopy = null;
            var _loc_2:* = new Array();
            for each (_loc_3 in this._groupTowerCopy)
            {
                
                if (_loc_3.enterMinLevel == param1)
                {
                    _loc_2[0] = _loc_3;
                    _loc_2[1] = _loc_3.maps.split("#");
                    return _loc_2;
                }
            }
            return [];
        }// end function

        public function getGroupTowerCopyByCode(param1:int) : TCopy
        {
            return this._groupTowerCopy[param1] as TCopy;
        }// end function

        public function getDefenseCopy(param1:int, param2:int) : Array
        {
            var _loc_5:TCopy = null;
            var _loc_3:* = new Array();
            var _loc_4:* = new Array();
            for each (_loc_5 in this._defenseCopy)
            {
                
                if (_loc_5.group != param2)
                {
                    continue;
                }
                if (_loc_5.enterMinLevel <= param1 && param1 <= _loc_5.enterMaxLevel)
                {
                    _loc_3.push(_loc_5);
                    continue;
                }
                _loc_4.push(_loc_5);
            }
            _loc_3.sort(this.sortCopy);
            _loc_4.sort(this.sortCopy2);
            return _loc_3.concat(_loc_4);
        }// end function

        private function sortCopy(param1:TCopy, param2:TCopy) : int
        {
            if (param1.enterMinLevel > param2.enterMinLevel)
            {
                return -1;
            }
            if (param1.enterMinLevel == param2.enterMinLevel)
            {
                if (param1.combat > param2.combat)
                {
                    return -1;
                }
                return 1;
            }
            else
            {
                return 1;
            }
        }// end function

        private function sortCopy2(param1:TCopy, param2:TCopy) : int
        {
            if (param1.enterMinLevel > param2.enterMinLevel)
            {
                return 1;
            }
            if (param1.enterMinLevel == param2.enterMinLevel)
            {
                if (param1.combat > param2.combat)
                {
                    return 1;
                }
                return -1;
            }
            else
            {
                return -1;
            }
        }// end function

        public function getSingleCopyByLevel(param1:int) : Array
        {
            var _loc_4:TCopy = null;
            var _loc_2:* = new Array();
            var _loc_3:* = new Array();
            for each (_loc_4 in this._singleCopy)
            {
                
                if (_loc_4.enterMinLevel <= param1 && param1 <= _loc_4.enterMaxLevel)
                {
                    _loc_2.push(_loc_4);
                    continue;
                }
                _loc_3.push(_loc_4);
            }
            _loc_2.sort(this.sortCopy);
            _loc_3.sort(this.sortCopy2);
            return _loc_2.concat(_loc_3);
        }// end function

        public function get defenseCopy() : Dictionary
        {
            return this._defenseCopy;
        }// end function

        public function isDefenseCopyMapId(param1:int) : Boolean
        {
            var _loc_2:TCopy = null;
            for each (_loc_2 in this._defenseCopy)
            {
                
                if (_loc_2.intoMapId == param1)
                {
                    return true;
                }
            }
            return false;
        }// end function

        public function get groupTowerCopy() : Dictionary
        {
            return this._groupTowerCopy;
        }// end function

        public static function get instance() : CopyConfig
        {
            if (_instance == null)
            {
                _instance = new CopyConfig;
            }
            return _instance;
        }// end function

    }
}
